package com.sojson.project.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.enums.core.EGeneralStatus;
import com.sojson.project.controller.param.verify.ArticleParamVerify;
import com.sojson.project.entity.po.Article;
import com.sojson.project.query.entity.bo.Select;
import com.sojson.project.service.ArticleService;
import com.sojson.result.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 文章分类表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "文章表管理")
@RestController
@RequestMapping(value = "/article")
public class ArticleController {

    @Resource
    private ArticleService articleServiceImpl;

    /**
     * 单条插入
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/article/insert')")
    @ApiOperation("添加文章")
    public Object insert(@RequestBody Article article) throws SQLException {
        // 校验参数
        Result<?> paramResult = ArticleParamVerify.insert(article);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        article.insertInit();
        articleServiceImpl.insert(article);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    // @PostMapping("/insertList")
    public Object insertList(@RequestBody Article article) throws SQLException {
        return Result.success(articleServiceImpl.insertList(article));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByDto")
    public Object deleteByDto(@RequestBody Article article) throws SQLException {
        return Result.success(articleServiceImpl.deleteByDto(article));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/countAll")
    public Object countAll() throws SQLException {
        int countAll = articleServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findAll")
    public Object findAll() throws SQLException {
        List<Article> findAll = articleServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    // @PostMapping("/countByDto")
    public Object countByDto(@RequestBody Article article) throws SQLException {
        int countByDto = articleServiceImpl.countByDto(article);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findByDto")
    @PreAuthorize("@ss.hasPermi('/article/findByDto')")
    @ApiOperation(value = "根据对象查询文章", response = Article.class)
    public Object findByDto(Article article) throws SQLException {
        List<Article> findByDto = articleServiceImpl.findByDto(article);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteById")
    public Object deleteById(Integer id) throws SQLException {
        articleServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @DeleteMapping("/deleteByIdList")
    @PreAuthorize("@ss.hasPermi('/article/deleteByIdList')")
    @ApiOperation("通过id列表删除文章(物理删除)")
    public Object deleteByIdList(@RequestParam("ids") ArrayList<Integer> ids) throws SQLException {
        if (ids.size() < 1) {
            return Result.success();
        }
        return Result.success(articleServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateById")
    public Object updateById(@RequestBody Article article) throws SQLException {
        articleServiceImpl.updateById(article);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticByIdList")
    public Object updateStaticByIdList(@RequestBody Article article) throws SQLException {
        return Result.success(articleServiceImpl.updateStaticByIdList(article));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangeByIdList")
    public Object updateChangeByIdList(@RequestBody Article article) throws SQLException {
        return Result.success(articleServiceImpl.updateChangeByIdList(article));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    @PutMapping("/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/article/updatePartialById')")
    @ApiOperation("通过id更新文章(局部更新)")
    public Object updatePartialById(@RequestBody Article article) throws SQLException {
        // 校验参数
        Result<?> paramResult = ArticleParamVerify.updatePartialById(article);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        article.updateInit();
        articleServiceImpl.updatePartialById(article);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticPartialByIdList")
    public Object updateStaticPartialByIdList(@RequestBody Article article) throws SQLException {
        return Result.success(articleServiceImpl.updateStaticPartialByIdList(article));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangePartialByIdList")
    public Object updateChangePartialByIdList(@RequestBody Article article) throws SQLException {
        return Result.success(articleServiceImpl.updateChangePartialByIdList(article));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping("/findById")
    @ApiOperation(value = "通过id查询文章", response = Article.class)
    public Object findById(Integer id) throws SQLException {
        Article findById = articleServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<Article> findByIdList = articleServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    // @PostMapping("/findIdByDto")
    public Object findIdByDto(@RequestBody Article article) throws SQLException {
        ArrayList<Integer> findIdByDto = articleServiceImpl.findIdByDto(article);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @PostMapping("/clear")
    public Object clear() throws SQLException {
        articleServiceImpl.clear();
        return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findAppByDto")
    @ApiOperation(value = "根据对象查询文章", response = Article.class)
    public Object findAppByDto(Article article) throws SQLException {
        article.setStatus(EGeneralStatus.NORMAL.getCode());
        List<Article> findByDto = articleServiceImpl.findByDto(article);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 根据对象查询下拉列表
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findSelect")
    @PreAuthorize("@ss.hasPermi('/article/findSelect')")
    @ApiOperation(value = "根据对象查询文章下拉列表", response = Select.class)
    public Object findSelect(Article article) throws SQLException {
        List<Select> findSelect = articleServiceImpl.findSelect(article);
        return Result.success(findSelect, findSelect);
    }

    /**
     * 根据对象查询下拉列表
     * 
     * @param article
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/selectApp")
    @ApiOperation(value = "根据对象查询文章下拉列表", response = Select.class)
    public Object selectApp(Article article) throws SQLException {
        article.setStatus(EGeneralStatus.NORMAL.getCode());
        List<Select> findSelect = articleServiceImpl.findSelect(article);
        return Result.success(findSelect, findSelect);
    }

}